package com.hh.service.service.impl.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.hh.common.constants.Constants;
import com.hh.common.enums.NowMoneyTransactionType;
import com.hh.common.model.finance.UserRecharge;
import com.hh.common.model.user.UserSmallChangeRecord;
import com.hh.common.page.CommonPage;
import com.hh.common.request.PageParamRequest;
import com.hh.common.response.NowMoneyRecordResponse;
import com.hh.common.response.UserRechargeRecordResponse;
import com.hh.common.utils.DateUtil;
import com.hh.common.utils.HhUtil;
import com.hh.service.dao.UserRechargeDao;
import com.hh.service.dao.UserSmallChangeRecordDao;
import com.hh.service.service.UserService;
import com.hh.service.service.user.UserSmallChangeRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserSmallChangeRecordServiceImpl extends ServiceImpl<UserSmallChangeRecordDao, UserSmallChangeRecord> implements UserSmallChangeRecordService {

    @Autowired
    private UserService userService;

    @Resource
    private UserSmallChangeRecordDao dao;

    @Resource
    private UserRechargeDao userRechargeDao;
    /**
     * 用户零钱记录
     * @param type
     * @param dateLimit
     * @param pageRequest
     * @return
     */
    @Override
    public CommonPage<NowMoneyRecordResponse> nowMoneyBillRecord(String type, String dateLimit, PageParamRequest pageRequest) {
        Integer uid = userService.getUserIdException();

        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());

        LambdaQueryWrapper<UserSmallChangeRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserSmallChangeRecord::getUid,uid);
        if("income".equals(type)){
            lqw.eq(UserSmallChangeRecord::getPm,1);
        }else if("expenditure".equals(type)){
            lqw.eq(UserSmallChangeRecord::getPm,2);
        }
        if(StrUtil.isNotBlank(dateLimit)){
            List<String> list = HhUtil.stringToArrayStr(dateLimit);
            String startTime = list.get(0);
            String endTime = list.get(1);
            startTime = DateUtil.appointedDayStrToFormatStr(startTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_START);
            endTime = DateUtil.appointedDayStrToFormatStr(endTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_END);
            lqw.between(UserSmallChangeRecord::getCreateTime,startTime,endTime);
        }
        List<UserSmallChangeRecord> userSmallChangeRecords = dao.selectList(lqw);


        CommonPage<UserSmallChangeRecord> userSmallChangeRecordCommonPage = CommonPage.restPage(userSmallChangeRecords);
        List<NowMoneyRecordResponse> responseList = CollUtil.newArrayList();

        for (UserSmallChangeRecord userSmallChangeRecord : userSmallChangeRecords) {
            NowMoneyRecordResponse nowMoneyRecordResponse = new NowMoneyRecordResponse();
            BeanUtil.copyProperties(userSmallChangeRecord,nowMoneyRecordResponse);
            for (NowMoneyTransactionType value : NowMoneyTransactionType.values()) {
                if(userSmallChangeRecord.getTransactionType().equals(value.getCode())){
                    nowMoneyRecordResponse.setTransactionTypeStr(value.getInfo());
                    break;
                }
            }
            responseList.add(nowMoneyRecordResponse);
        }

        CommonPage<NowMoneyRecordResponse> detailPage = CommonPage.restPage(responseList);
        BeanUtils.copyProperties(userSmallChangeRecordCommonPage, detailPage, "list");
        return detailPage;
    }

    @Override
    public CommonPage<UserRechargeRecordResponse> record(String dateLimit, PageParamRequest pageRequest) {
        Integer uid = userService.getUserIdException();

        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());

        LambdaQueryWrapper<UserRecharge> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRecharge::getUid,uid);

        if(StrUtil.isNotBlank(dateLimit)){
            List<String> list = HhUtil.stringToArrayStr(dateLimit);
            String startTime = list.get(0);
            String endTime = list.get(1);
            startTime = DateUtil.appointedDayStrToFormatStr(startTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_START);
            endTime = DateUtil.appointedDayStrToFormatStr(endTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_END);
            lqw.between(UserRecharge::getCreateTime,startTime,endTime);
        }
        List<UserRecharge> userRecharges = userRechargeDao.selectList(lqw);


        CommonPage<UserRecharge> userRechargeCommonPage = CommonPage.restPage(userRecharges);
        List<UserRechargeRecordResponse> responseList = CollUtil.newArrayList();

        for (UserRecharge userRecharge : userRecharges) {
            UserRechargeRecordResponse userRechargeRecordResponse = new UserRechargeRecordResponse();
            BeanUtil.copyProperties(userRecharge,userRechargeRecordResponse);
            responseList.add(userRechargeRecordResponse);
        }

        CommonPage<UserRechargeRecordResponse> detailPage = CommonPage.restPage(responseList);
        BeanUtils.copyProperties(userRechargeCommonPage, detailPage, "list");
        return detailPage;
    }
}
